﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PreeRegisterModel;
using System.Collections.Specialized;
using PreeRegisterBLL;
using PreeRegisterBLL.Security;

namespace PreeRegister_WebAPI.Controllers
{
    public class sysncController : ApiController
    {
        [HttpPost]
        /// <summary>
        /// 心跳接口
        /// http://192.168.1.159:8080/api/sysnc/heartbeat
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel heartbeat(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                HeartbeatModel HeartbeatModel_Response = new HeartbeatModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.HeartbeatModel_Response(HeartbeatModel_Response);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sysnc|heartbeat|Post|\r\n" + body_json + "\r\n\r\n");

                //获取服务器时间
                HeartbeatModel_Response.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");// CommonBLL.GetSystemTime().ToString("yyyy-MM-dd HH:mm:ss");

                //序列化返回内容类
                ReturnBodyJson = csSerialize.HeartbeatModel_Response(HeartbeatModel_Response);                

                //返回参数日志
                CommonBLL.WriteLog("sysnc|heartbeat|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                HeartbeatModel HeartbeatModel_Response = new HeartbeatModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.HeartbeatModel_Response(HeartbeatModel_Response);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|heartbeat|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }


        [HttpPost]
        /// <summary>
        /// 排班号源统计接口
        /// http://192.168.1.159:8080/api/sysnc/getcount
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel getcount(NameValueCollection any)
        {
            try
            {
                //获取接口接收时间
                string rreceivetime = CommonBLL.GetSystemTime().ToString("yyyy-MM-dd HH:mm:ss");

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                WorkGetcountResponseBodyModel workGetcountResponseBodyModel = new WorkGetcountResponseBodyModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkGetcountResponseBodyModel(workGetcountResponseBodyModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sysnc|getcount|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkGetcountRequestBodyModel workGetcountRequestBodyModel = csSerialize.WorkGetcountRequestBodyModel(body_json);

                //获取平台调用接口时间
                string sendertime = workGetcountRequestBodyModel.sendertime;

                //查询排班号源统计数量情况
                workGetcountResponseBodyModel = WorkGetcountBLL.GetWorkCount(workGetcountRequestBodyModel);

                //获取接口发送时间
                string rsendtime = CommonBLL.GetSystemTime().ToString("yyyy-MM-dd HH:mm:ss");

                //赋值接收发送时间
                workGetcountResponseBodyModel.sendertime = sendertime;
                workGetcountResponseBodyModel.rreceivetime = rreceivetime;
                workGetcountResponseBodyModel.rsendtime = rsendtime;

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkGetcountResponseBodyModel(workGetcountResponseBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|getcount|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                WorkGetcountResponseBodyModel workGetcountResponseBodyModel = new WorkGetcountResponseBodyModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkGetcountResponseBodyModel(workGetcountResponseBodyModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|getcount|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }


        [HttpPost]
        /// <summary>
        /// 数据比对接口
        /// http://192.168.1.159:8080/api/sysnc/compare
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel compare(NameValueCollection any)
        {
            try
            {
                //获取接口接收时间
                string rreceivetime = CommonBLL.GetSystemTime().ToString("yyyy-MM-dd HH:mm:ss");

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                WorkCompareResponseModel workCompareResponseModel = new WorkCompareResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkCompareResponseModel(workCompareResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sysnc|compare|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkCompareRequestModel workCompareRequestModel = csSerialize.WorkCompareRequestModel(body_json);

                //获取平台调用接口时间
                string senddate = workCompareRequestModel.senddate;

                //查询排班号源比对情况
                workCompareResponseModel = WorkCompareBLL.GetWorkCompare(workCompareRequestModel);

                //获取接口发送时间
                string rsendtime = CommonBLL.GetSystemTime().ToString("yyyy-MM-dd HH:mm:ss");

                //赋值接收发送时间
                workCompareResponseModel.sendertime = senddate;
                workCompareResponseModel.rreceivetime = rreceivetime;
                workCompareResponseModel.rsendtime = rsendtime;

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkCompareResponseModel(workCompareResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|compare|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                WorkCompareResponseModel workCompareResponseModel = new WorkCompareResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkCompareResponseModel(workCompareResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|compare|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }


        [HttpPost]
        /// <summary>
        /// 排班号源排查接口
        /// http://192.168.1.159:8080/api/sysnc/work
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel work(NameValueCollection any)
        {
            try
            {

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                WorkDetailResponseModel workDetailResponseModel = new WorkDetailResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.WorkDetailResponseModel(workDetailResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sysnc|work|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                WorkDetailRequestModel workDetailRequestModel = csSerialize.WorkDetailRequestModel(body_json);

                //查询排班号源情况
                workDetailResponseModel = WorkDetailBLL.GetWorkDetail(workDetailRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.WorkDetailResponseModel(workDetailResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|work|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                WorkDetailResponseModel workDetailResponseModel = new WorkDetailResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.WorkDetailResponseModel(workDetailResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|work|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }


        [HttpPost]
        /// <summary>
        /// 获取订单
        /// http://192.168.1.159:8080/api/sysnc/orders
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel orders(NameValueCollection any)
        {
            try
            {

                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                OrderCompareResponseModel orderCompareResponseModel = new OrderCompareResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.OrderCompareResponseModel(orderCompareResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sysnc|orders|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                OrderCompareRequestModel orderCompareRequestModel = csSerialize.OrderCompareRequestModel(body_json);

                //查询排班号源情况
                orderCompareResponseModel = OrderCompareBLL.GetOrderCompare(orderCompareRequestModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.OrderCompareResponseModel(orderCompareResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|orders|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                OrderCompareResponseModel orderCompareResponseModel = new OrderCompareResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.OrderCompareResponseModel(orderCompareResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sysnc|orders|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }
    }
}
